package Frame.screen;

import FCRendering.FCRBattleOrderConstant;
import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import Function.Utils.SwingUtil;
import VWorldGameStudio.FCItem;
import VWorldGameStudio.FCPlayerBattleWrap;
import VWorldGameStudio.FCPlayerInfo;
import VWorldGameStudio.FCTank;
import Function.FCItemForTankHelper;
import Function.FCMutilPlayCache;
import utils.FCUIConfig;
import utils.entity.UIConfigEntity;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import static FCRendering.FCRMaster.*;

public class FightTankMenuScreen extends IScreen implements IRListener {
    private Font font1 = null;
    String[] fightMenuBnt = new String[]{"fightMenuBnt1", "fightMenuBnt2", "fightMenuBnt3", "fightMenuBnt4", "fightMenuBnt5", "fightMenuBnt6"};

    String[] funtionNames = new String[]{
            "主炮(空格)", "道具(D)", "副炮(A)", "炮弹(F)", "S-E(S)", "辅助"
    };
    String[] fuzhuMenuBnt = new String[]{"fightMenuFuzhu1", "fightMenuFuzhu2", "fightMenuFuzhu3", "fightMenuFuzhu4"};
    String[] fuzhuMenu = new String[]{"乘降(Z)", "逃跑(R)", "防御(X)", "保护"};

    private boolean showNames = false;
    private boolean showTips = false;
    private String tips = "";
    private ArrayList<String> canUseItemTypes = new ArrayList<>(4);

    // private ArrayList<String> canUseTankItemTypes = new ArrayList<>(1);
    public FightTankMenuScreen() {
        canUseItemTypes.add("food");
        canUseItemTypes.add("drug");
        canUseItemTypes.add("tool");
        canUseItemTypes.add("throw");
        canUseItemTypes.add("plate");

    }

    @Override
    public void listen_mouse_click_game(MouseEvent g) {
        if (!fcv.isBattleReady()) {
            return;
        }
        int x = g.getX(), y = g.getY();
        for (int i = 0; i < 6; i++) {
            if (FCUIConfig.getIn().checkUIClick(fightMenuBnt[i], x, y)) {
                if (i != 3) {//不让3炮弹走这里
                    clickTankWhat(i);
                }
                break;
            }
        }
        FCPlayerBattleWrap cp = fcv.getBattleWraps().get(fcv.getFightSelectPlayer());
        if (fcv.getFightMenuBntSelect() == 5) {
            for (int i = 0; i < 4; i++) {
                if (FCUIConfig.getIn().checkUIClick(fuzhuMenuBnt[i], x, y)) {
                    if (showNames) {
                        //TODO 要是显示名字的话，fuzhuMenuBnt按键就代表了每个名字，所以点的是名字了
                        if (i < fcv.getBattleWraps().size()) {
                            cp.setBattleAttackOrder(FCRBattleOrderConstant.Order_Protect);
                            cp.setBattlePlayerSelectPlayer(fcv.getBattleWraps().get(i).getHostUid() + "," + fcv.getBattleWraps().get(i).getName());
                            showNames = false;
                            cp.setBattleReady(true);
                            selectNextPlayer();
                        }
                    } else {
                        //展示其他页面
                        if (i == 0) {
                            //上车,有车才能做
                            FCPlayerInfo playerInfo = FCMutilPlayCache.findPlayerByName(cp.getHostUid(), cp.getName());
                            if (playerInfo != null && !playerInfo.getPlayertank1().equals("null")) {
                                cp.setBattleDriverTank(false);
                                cp.checkAndSyncBattleWrap();
                                setFightMenuBnt(-1);
                            }
                        } else if (i == 1) {
                            //逃跑指令
                            cp.setBattleAttackOrder(FCRBattleOrderConstant.Order_Run);
                            cp.setBattleReady(true);
                            selectNextPlayer();
                        } else if (i == 2) {
                            //防卫指令
                            cp.setBattleAttackOrder(FCRBattleOrderConstant.Order_Defense);
                            cp.setBattleReady(true);
                            selectNextPlayer();
                        } else {
                            //保护他人
                            showNames = true;
                        }
                    }
                    break;
                }
            }
        }

        //TODO 要是点了使用物品
        if (fcv.isBattleReady()) {
            if (y > (int) (1016 * FCRMaster.fcv.getProportion()) && y < (int) (1066 * FCRMaster.fcv.getProportion())) {
                for (int i = 0; i < 10; i++) {
                    if (x > (int) ((647 + i * 64) * FCRMaster.fcv.getProportion()) && x < (int) ((697 + i * 64) * FCRMaster.fcv.getProportion())) {
                        //使用物品 检查这个位置是不是空的物品
                        FCPlayerInfo playerInfo = FCMutilPlayCache.findPlayerByName(cp.getHostUid(), cp.getName());
                        if(playerInfo == null){
                            continue;
                        }
                        String item = playerInfo.getKnapsackItemIDOfNum(i);
                        if (item != null && item.length() > 0) {
                            FCItem fcitem = itemLib.getItemOfID(item);
                            if (canUseItemTypes.contains(fcitem.getType())) {
                                cp.setBattlePlayerSelectItem(i);
                                cp.setBattlePlayerSelectItemMode(1);
                                cp.setBattleAttackOrder(FCRBattleOrderConstant.Order_USEITEM);
                                fcv.setBattlePlayerSelect(true);
                            } else {
                                fcv.getMessage().add("该道具无法战斗中使用!");
                                fcv.showMessage();
                            }
                        }
                        break;
                    }
                }
            }

            if (fcv.getBattleShowingList().size() > 0) {
                int i = 0;
                int y1 = 0;
                for (i = 0; i < fcv.getBattleShowingList().size(); i++) {
                    y1 = (int) ((19 + 44 * i) * fcv.getProportion()) + fcv.getBattleShowListY();
                    if (x > fcv.getBattleShowListX() && x < fcv.getBattleShowListX() + 250 * fcv.getProportion()) {
                        if (y > y1 && y < y1 + 44 * fcv.getProportion()) {
                            //TODO 这里有可以是道具也可以是炮弹,有，的就是炮弹, @号的是道具物品,人类的就只有道具，没坦克道具和炮弹
                            String text = fcv.getBattleShowingList().get(i);
                            if (text.contains("@")) {//人类背包道具
                                FCItem fcitem = itemLib.getItemOfID(text.split("@")[0]);
                                if (canUseItemTypes.contains(fcitem.getType())) {
                                    cp.setBattlePlayerSelectItemMode(1);
                                    cp.setBattlePlayerSelectItem(Integer.parseInt(text.split("@")[1]));
                                    cp.setBattleAttackOrder(FCRBattleOrderConstant.Order_USEITEM);
                                    fcv.setBattlePlayerSelect(true);
                                    fcv.getBattleShowingList().clear();
                                } else {
                                    fcv.getMessage().add("该道具无法战斗中使用!");
                                    fcv.showMessage();
                                }
                            } else if (text.contains(";")) {//坦克背包道具
                                FCItem fcitem = itemLib.getItemOfID(text.split(";")[0]);
                                if (canUseItemTypes.contains(fcitem.getType())) {
                                    cp.setBattlePlayerSelectItemMode(2);
                                    cp.setBattlePlayerSelectItem(Integer.parseInt(text.split(";")[1]));
                                    cp.setBattleAttackOrder(FCRBattleOrderConstant.Order_USEITEM);
                                    fcv.setBattlePlayerSelect(true);
                                    fcv.getBattleShowingList().clear();
                                } else {
                                    fcv.getMessage().add("该道具无法战斗中使用!");
                                    fcv.showMessage();
                                }
                            } else if (text.contains("-")) {
                                //炮弹
                                cp.setBattlePlayerSelectPaoDanIndex(Integer.parseInt(text.split("-")[2]));
                                cp.setBattlePlayerSelectMenu(FCRBattleOrderConstant.BattleSelectMenuTankMainGunP);
                                cp.setBattleAttackOrder(FCRBattleOrderConstant.Order_Attack);
                                fcv.setBattlePlayerSelect(true);
                                fcv.getBattleShowingList().clear();
                            }
                            break;
                        }
                    }
                }
            }
        }
    }


    private void forceDownTank() {//用了别人得坦克时，别人断线了，强制下车
        FCPlayerBattleWrap cp = fcv.getBattleWraps().get(fcv.getFightSelectPlayer());
        cp.setBattleDriverTank(false);
        cp.checkAndSyncBattleWrap();
        setFightMenuBnt(-1);
        fcv.getBattleShowingList().clear();
    }

    private void clickTankWhat(int i) {
        setFightMenuBnt(i);
        FCPlayerBattleWrap cp = fcv.getBattleWraps().get(fcv.getFightSelectPlayer());
        FCPlayerInfo pl = FCMutilPlayCache.findPlayerByName(cp.getHostUid(), cp.getName());
        if(pl == null){
            selectNextPlayer();
           return;
        }
        FCTank tank = tankLib.getTankOfID(pl.getPlayertank1(), pl.getPlayertankHostId());
        if (tank == null) {
            forceDownTank();
            return;
        }
        boolean canAttack = false;
        switch (fcv.getFightMenuBntSelect()) {
            case 0://TODO 那就是点了主炮
                if (!tank.isCdeviceOk()) {
                    fcv.getMessage().add(
                            FCRConstants.SYSTEM + FCRConstants.BATTLE + " C装置无法使用");
                    fcv.showMessage();
                    return;
                }
                if (!tank.isGun1Ok()) {
                    fcv.getMessage().add(
                            FCRConstants.SYSTEM + FCRConstants.BATTLE + " 没装备主炮或主炮已损坏");
                    fcv.showMessage();
                    return;
                }
                //还要判断有没有弹药
                FCItem gun1 = itemLib.getItemOfID(tank.getGunID());
                if (gun1.getCapacity() > 0 && gun1.getCurrentCapacityVal() == 0) {
                    //TODO 就是不是无限子弹的但现在弹数为0 了
                    fcv.getMessage().add(FCRConstants.SYSTEM + FCRConstants.BATTLE + " 主炮已经弹尽了");
                    fcv.showMessage();
                    return;
                }
                cp.setBattleAttackOrder(FCRBattleOrderConstant.Order_Attack);
                cp.setBattlePlayerSelectMenu(FCRBattleOrderConstant.BattleSelectMenuTankMainGun);
                fcv.setBattlePlayerSelect(true);
                break;
            case 1://TODO 道具

                break;
            case 2://TODO 副炮
                if (!tank.isGun2Ok()) {
                    fcv.getMessage().add(
                            FCRConstants.SYSTEM + FCRConstants.BATTLE + " 没装备副炮或副炮已损坏");
                    fcv.showMessage();
                    return;
                }
                cp.setBattleAttackOrder(FCRBattleOrderConstant.Order_Attack);
                cp.setBattlePlayerSelectMenu(FCRBattleOrderConstant.BattleSelectMenuTankFuGun);
                fcv.setBattlePlayerSelect(true);
                break;
            case 3://TODO 炮弹
                if (!tank.isCdeviceOk()) {
                    fcv.getMessage().add(
                            FCRConstants.SYSTEM + FCRConstants.BATTLE + " C装置无法使用");
                    fcv.showMessage();
                    return;
                }
                if (!tank.isGun1Ok()) {
                    fcv.getMessage().add(
                            FCRConstants.SYSTEM + FCRConstants.BATTLE + " 没装备主炮或主炮已损坏");
                    fcv.showMessage();
                    return;
                }
                if (!tank.isDiPanOk()) {
                    fcv.getMessage().add(
                            FCRConstants.SYSTEM + FCRConstants.BATTLE + " 底盘已损坏,无法发射炮弹");
                    fcv.showMessage();
                    return;
                }
                FCItem dipan = itemLib.getItemOfID(tank.getChassisID());
                fcv.getBattleShowingList().addAll(FCItemForTankHelper.getShellList(dipan));
                break;
            case 4://TODO S_E
                if (!tank.isCdeviceOk()) {
                    fcv.getMessage().add(
                            FCRConstants.SYSTEM + FCRConstants.BATTLE + " C装置无法使用");
                    fcv.showMessage();
                    return;
                }
                if (!tank.isSEOk()) {
                    fcv.getMessage().add(
                            FCRConstants.SYSTEM + FCRConstants.BATTLE + " 没装备SE或SE已损坏");
                    fcv.showMessage();
                    return;
                }
                //还要判断有没有弹药
                FCItem se = itemLib.getItemOfID(tank.getSEID());
                if (se.getCapacity() > 0 && se.getCurrentCapacityVal() == 0) {
                    //TODO 就是不是无限子弹的但现在弹数为0 了
                    fcv.getMessage().add(FCRConstants.SYSTEM + FCRConstants.BATTLE + " SE已经弹尽了");
                    fcv.showMessage();
                    return;
                }
                cp.setBattleAttackOrder(FCRBattleOrderConstant.Order_Attack);
                cp.setBattlePlayerSelectMenu(FCRBattleOrderConstant.BattleSelectMenuTankSE);
                fcv.setBattlePlayerSelect(true);
                break;
        }
    }

    @Override
    public void listen_mouse_move_game(MouseEvent g) {
        int x = g.getX(), y = g.getY();
        int findOne = -1;
        for (int i = 0; i < 6; i++) {
            if (FCUIConfig.getIn().checkUIClick(fightMenuBnt[i], x, y)) {
                findOne = 90 + i;
                break;
            }
        }
        if (findOne != -1) {
            fcv.setEnterBu(findOne);
            return;
        }
        switch (fcv.getFightMenuBntSelect()) {
            case 0://TODO 攻击

                break;
            case 1:

                break;
            case 5://TODO 辅助
                for (int i = 0; i < 4; i++) {
                    if (FCUIConfig.getIn().checkUIClick(fuzhuMenuBnt[i], x, y)) {
                        if (showNames) {
                            findOne = 110 + i;
                        } else {
                            findOne = 100 + i;
                        }
                    }
                }
                break;
            default:
                break;
        }
        if (findOne != -1) {
            fcv.setEnterBu(findOne);
        } else {
            fcv.setEnterBu(0);
        }
    }

    @Override
    public void dispatchPaintComponent(Graphics g, FCRMaster fcrMaster) {
        g.setFont(font1);

        for (int i = 0; i < 6; i++) {
            UIConfigEntity fightMenuName = FCUIConfig.getIn().getByKey(fightMenuBnt[i]);

            if (fcv.getFightMenuBntSelect() == i) {
                g.setColor(Color.WHITE);
            } else {
                if (fcv.getEnterBu() == 90 + i) {
                    g.setColor(Color.BLUE);
                } else {
                    g.setColor(Color.BLACK);
                }
            }

            int y = (int) (fightMenuName.getUiRect().y + (fightMenuName.getUiRect().height + 26 * fcv.getProportion()) / 2);
            int x = (int) (fightMenuName.getUiRect().x + (fightMenuName.getUiRect().width - SwingUtil.getStringWidht(funtionNames[i], font1)) / 2);
            g.drawString(funtionNames[i], x, y);
        }

        if (fcv.getFightMenuBntSelect() != -1) {
            FCPlayerBattleWrap playerBattleWrap = fcv.getBattleWraps().get(fcv.getFightSelectPlayer());

            if (playerBattleWrap.getHostUid().equals(FCRMaster.gameSave.HOST_UID)) {
                FCPlayerInfo playerInfo = FCMutilPlayCache.findPlayerByName(playerBattleWrap.getHostUid(), playerBattleWrap.getName());
                if (playerInfo != null && !playerInfo.isDeath()) {
                    switch (fcv.getFightMenuBntSelect()) {
                        case 5://TODO 辅助
                            if (!showNames) {
                                for (int i = 0; i < 4; i++) {
                                    if (fcv.getEnterBu() == 100 + i) {
                                        g.setColor(Color.BLUE);
                                    } else {
                                        g.setColor(Color.WHITE);
                                    }
                                    UIConfigEntity entity = FCUIConfig.getIn().getByKey(fuzhuMenuBnt[i]);
                                    g.drawString(fuzhuMenu[i], entity.getUiRect().x, (int) (entity.getUiRect().y + 20 * fcv.getProportion()));
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
            }

            if (showNames) {
                for (int i = 0; i < fcv.getBattleWraps().size(); i++) {
                    FCPlayerBattleWrap w = fcv.getBattleWraps().get(i);
                    if (fcv.getEnterBu() == 110 + i) {
                        g.setColor(Color.BLUE);
                    } else {
                        g.setColor(Color.WHITE);
                    }
                    UIConfigEntity entity = FCUIConfig.getIn().getByKey(fuzhuMenuBnt[i]);
                    g.drawString(w.getName(), entity.getUiRect().x, (int) (entity.getUiRect().y + 20 * fcv.getProportion()));
                }
            }
        }

        if (showTips) {
            g.setColor(Color.WHITE);
            g.drawString(tips, (int) (1596 * fcv.getProportion()), (int) (960 * fcv.getProportion()));
        }
    }

    @Override
    public void setBounds() {
        font1 = new Font(FCRConstants.FONT_BLACK, 0, (int) (26 * fcv.getProportion()));
    }

    //攻击，道具，辅助哪些按键
    private void setFightMenuBnt(int i) {
        fcv.setFightMenuBntSelect(i);
        switch (i) {
            case 0:
                showTips = false;
                break;
            case 1:
                showTips = true;
                tips = "使用请点击底部物品栏";
                break;
            case 2:
                showTips = false;
                break;
            default:
                showTips = false;
                break;
        }
        showNames = false;
    }

    public void resetUI() {
        showTips = false;
        showNames = false;
    }

    //切换到下一个角色
    public void selectNextPlayer() {
        for (int i = 0; i < fcv.getBattleWraps().size(); i++) {
            FCPlayerBattleWrap playerBattleWrap = fcv.getBattleWraps().get(i);
            if (playerBattleWrap.getHostUid().equals(FCRMaster.gameSave.HOST_UID) && !playerBattleWrap.isBattleReady()) {
                fcv.setFightMenuBntSelect(-1);
                fcv.setFightSelectPlayer(i);
                FCRMaster.fcv.setShowWhosBagItem(playerBattleWrap.getName());
                resetUI();
                break;
            }
        }
    }

    //{ "up", "down", "left", "right", "say", "lookinto", "give", "menu","Q","space","Z","X","C","R"}
    @Override
    public void listen_key_press_game(String key) {
        //System.out.println(" tankemenu "+key);
        if (!fcv.isBattleReady()) {
            return;
        }
        FCPlayerBattleWrap cp = fcv.getBattleWraps().get(fcv.getFightSelectPlayer());
        if ("space".equals(key)) {
            clickTankWhat(0);
            fcv.getBattleShowingList().clear();
        } else if ("Z".equals(key)) {
            FCPlayerInfo playerInfo = FCMutilPlayCache.findPlayerByName(cp.getHostUid(), cp.getName());
            if (playerInfo != null && !playerInfo.getPlayertank1().equals("null")) {
                cp.setBattleDriverTank(false);
                cp.checkAndSyncBattleWrap();
                setFightMenuBnt(-1);
            }
            fcv.setBattlePlayerSelect(false);
            fcv.getBattleShowingList().clear();
        } else if ("down".equals(key)) {
            clickTankWhat(4);
            fcv.getBattleShowingList().clear();
        } else if ("give".equals(key)) {
            //逃跑指令
            cp.setBattleAttackOrder(FCRBattleOrderConstant.Order_Run);
            cp.setBattleReady(true);
            fcv.setBattlePlayerSelect(false);
            selectNextPlayer();
            fcv.getBattleShowingList().clear();
        } else if ("left".equals(key)) {
            clickTankWhat(2);
            fcv.getBattleShowingList().clear();
        } else if ("X".equals(key)) {
            //防御
            cp.setBattleAttackOrder(FCRBattleOrderConstant.Order_Defense);
            cp.setBattleReady(true);
            fcv.setBattlePlayerSelect(false);
            selectNextPlayer();
            fcv.getBattleShowingList().clear();
        } else if ("right".equals(key)) {
            //道具
            if (fcv.getBattleShowingList().size() > 0) {
                fcv.getBattleShowingList().clear();
                fcv.setBattlePlayerSelect(false);
            } else {
                //还有人物背包的道具
                FCPlayerInfo playerInfo = FCMutilPlayCache.findPlayerByName(cp.getHostUid(), cp.getName());
                for (int i = 0; i < 10; i++) {
                    String itemidd = playerInfo.getKnapsackItemIDOfNum(i);
                    if (itemidd != null && !itemidd.equals("null")) {
                        fcv.getBattleShowingList().add(itemidd + "@" + i);
                    }
                }
                //还有坦克背包的道具
                FCTank tank = tankLib.getTankOfID(playerInfo.getPlayertank1(), playerInfo.getPlayertankHostId());
                if(tank == null){
                    forceDownTank();
                    return;
                }
                for (int i = 0; i < 10; i++) {
                    String tItem = tank.getKnapsack()[i];
                    if (tItem != null && !tItem.equals("null")) {
                        fcv.getBattleShowingList().add(tItem + ";" + i);
                    }
                }
                if (fcv.getBattleShowingList().size() == 0) {
                    fcv.getMessage().add("人物没有道具物品可以使用!");
                    fcv.showMessage();
                    fcv.setBattlePlayerSelect(false);
                }
            }
            clickTankWhat(1);
        } else if ("say".equals(key)) {
            //炮弹
            if (fcv.getBattleShowingList().size() > 0) {
                fcv.getBattleShowingList().clear();
                fcv.setBattlePlayerSelect(false);
            } else {
                clickTankWhat(3);

                if (fcv.getBattleShowingList().size() == 0) {
                    fcv.getMessage().add("坦克没有承载的炮弹可以使用!");
                    fcv.showMessage();
                    fcv.setBattlePlayerSelect(false);
                }
            }
        } else {
            fcv.setBattlePlayerSelect(false);
            fcv.getBattleShowingList().clear();
        }
    }
}
